package com.lysh.aliHttp.common;

import com.lysh.aliHttp.common.enums.HttpMethod;
import com.lysh.aliHttp.model.WebServiceRequest;

import java.net.URI;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author liyishan
 * @date 2023/5/5 09:22
 * @apiNote
 */

public class RequestMessage extends HttpMesssage{
    private String bucket;
    private String key;
    private String resourcePath;
    private URI endpoint;
    private HttpMethod method;
    private Map<String, String> parameters;
    private URL absoluteUrl;
    private boolean useUrlSignature;
    private boolean useChunkEncoding;
    private final WebServiceRequest originalRequest;

    public RequestMessage(String bucketName, String key) {
        this((WebServiceRequest)null, bucketName, key);
    }

    public RequestMessage(WebServiceRequest originalRequest, String bucketName, String key) {
        this.method = HttpMethod.GET;
        this.parameters = new LinkedHashMap();
        this.useUrlSignature = false;
        this.useChunkEncoding = false;
        this.originalRequest = originalRequest == null ? WebServiceRequest.NOOP : originalRequest;
    }

    public HttpMethod getMethod() {
        return this.method;
    }

    public void setMethod(HttpMethod method) {
        this.method = method;
    }

    public URI getEndpoint() {
        return this.endpoint;
    }

    public void setEndpoint(URI endpoint) {
        this.endpoint = endpoint;
    }

    public String getResourcePath() {
        return this.resourcePath;
    }

    public void setResourcePath(String resourcePath) {
        this.resourcePath = resourcePath;
    }

    public Map<String, String> getParameters() {
        return this.parameters;
    }

    public void setParameters(Map<String, String> parameters) {
        this.parameters.clear();
        if (parameters != null && !parameters.isEmpty()) {
            this.parameters.putAll(parameters);
        }

    }

    public void addParameter(String key, String value) {
        this.parameters.put(key, value);
    }

    public void removeParameter(String key) {
        this.parameters.remove(key);
    }

    public boolean isRepeatable() {
        return this.getContent() == null || this.getContent().markSupported();
    }

    public String toString() {
        return "Endpoint: " + this.getEndpoint().getHost() + ", ResourcePath: " + this.getResourcePath() + ", Headers:" + this.getHeaders();
    }

    public URL getAbsoluteUrl() {
        return this.absoluteUrl;
    }

    public void setAbsoluteUrl(URL absoluteUrl) {
        this.absoluteUrl = absoluteUrl;
    }

    public boolean isUseUrlSignature() {
        return this.useUrlSignature;
    }

    public void setUseUrlSignature(boolean useUrlSignature) {
        this.useUrlSignature = useUrlSignature;
    }

    public boolean isUseChunkEncoding() {
        return this.useChunkEncoding;
    }

    public void setUseChunkEncoding(boolean useChunkEncoding) {
        this.useChunkEncoding = useChunkEncoding;
    }

    public WebServiceRequest getOriginalRequest() {
        return this.originalRequest;
    }

    public String getBucket() {
        return this.bucket;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }

    public String getKey() {
        return this.key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}
